<?php
if (!defined('FosfoaSystem')) {
    exit(1);
}
import($GLOBALS['spConfig']['controller_path'] . '/general.php');
class customers extends general
{
    public function __construct()
    {
        parent::__construct();
        $this->tpl_title = T("Dashboard");
    }
    function index()
    {
        $this->tt = "顾客管理";
        $customers_id = $this->spArgs("customers_id", 0);
        $current_page = $this->spArgs("page", 1);
        $page_size = $this->spArgs("size", 30);
        $obj = spClass("customersModel");
        if ($customers_id == 0) {
            $conditions = array('1' => 1);
        } else {
            $conditions = array('customers_id' => $customers_id);
        }
        $arr = $obj->spPager($current_page, $page_size)->findAll($conditions,
            'customers_id desc');
        $arrBar = $obj->spPager()->getPager();
        $this->tPageBar = $arrBar;
        $this->arr = $arr;
        $this->customers_id = $customers_id;
        $this->display("admin/customers_index.html");
    }
    function view()
    {
        $this->size = array(
            0 => '鞋类',
            1 => '包类',
            2 => '衣服');
        $customers_id = $this->spArgs("customers_id", 0);
        $Obj = spClass('customersModel');
        $conditions = array("customers_id" => $customers_id);
        $customers = $Obj->find($conditions);
        $customers['add'] = spClass('address_bookModel')->find('address_book_id=' . $customers['customers_default_address_id']);
        $customers['country'] = spClass('countriesModel')->findALL();
        $this->arr = $arr;
        $this->customers = $customers;
        $this->addtitle = "修改信息";
        $this->display("admin/customers_view.html");
    }
    function save()
    {
        $strAction = $this->spArgs("action");
        $customers_default_address_id = $this->spArgs("customers_default_address_id");
        $data = array(
            "customers_firstname" => $this->spArgs("customers_firstname"),
            "customers_lastname" => $this->spArgs("customers_lastname"),
            "customers_telephone" => $this->spArgs("customers_telephone"),
            );

        $dataadd = array(
            "customers_name" => $this->spArgs("customers_firstname") . ' ' . $this->spArgs("customers_lastname"),
            "entry_street_address" => $this->spArgs("entry_street_address"),
            "entry_suburb" => $this->spArgs("entry_suburb"),
            "entry_postcode" => $this->spArgs("entry_postcode"),
            "entry_city" => $this->spArgs("entry_city"),
            "entry_state" => $this->spArgs("entry_state"),
            "entry_country_id" => $this->spArgs("entry_country_id"),
            );

        $Obj = spClass('customersModel');
        $addressobj = spClass('address_bookModel');
        $customers_id = $this->spArgs("customers_id");
        $condition = array('customers_id' => $customers_id);
        $Obj->update($condition, $data);
        $addressobj->update('address_book_id=' . $customers_default_address_id, $dataadd);
        $msg = "顾客" . $customers_id . " 修改成功";
        Savelog($msg);
        $result = array(
            'status' => 'y',
            'info' => $msg,
            );
        echo json_encode($result);
    }
    function sh()
    {
        $Obj = spClass("customersModel");
        $id2 = $this->spArgs("customers_id", 0);
        $type = $this->spArgs("type", 0);
        $cid = $this->spArgs("cid", 0);
        $data = array('type' => $type);
        if ($id2 == 0) {
            $ids = $this->spArgs("selids");
            $idsarray = explode(',', $ids);

            foreach ($idsarray as $id) {
                $conditions = array('customers_id' => $id);
                $Obj->spLinker()->update($conditions, $data);
            }
        } else {

            $conditions = array('customers_id' => $id2);
            $Obj->spLinker()->update($conditions, $data);
        }
        $this->success(T('Successfully edited!'), spUrl("customers", "index"));

    }
    function del()
    {
        $Obj = spClass("customersModel");
        $customers_basket = spClass("customers_basketModel");
        $id2 = $this->spArgs("customers_id", 0);
        $cid = $this->spArgs("cid", 0);
        if ($id2 == 0) {
            $ids = $this->spArgs("selids");
            $idsarray = explode(',', $ids);
            foreach ($idsarray as $id) {
                $conditions = array('customers_id' => $id);
                $Obj->spLinker()->delete($conditions); // 删除记录
                $customers_basket->delete($conditions); //删除购物车
            }
        } else {
            $conditions = array('customers_id' => $id2);
            $Obj->spLinker()->delete($conditions); // 删除记录
            $customers_basket->delete($conditions); //删除购物车
        }
        $msg = "删除顾客" . $id2 . "信息";
        Savelog($msg);
        $this->success(T('Successfully  deleted!'), spUrl("customers", "index"));
    }
}